package com.pengtu.utils.web;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import com.pengtu.model.ProjectModel;

public class ControlHelpUtil {

	/**
	 * 设置Session
	 * 
	 * @param projectModel
	 */
	public static void setProjectToSession(final ProjectModel projectModel) {
		Struts2Utils.getSession().setAttribute("projectModel", projectModel);
	}

	public static void setProjectToRequest(final ProjectModel projectModel) {
		Struts2Utils.getRequest().setAttribute("projectModel", projectModel);
	}

	public static void setObjectToSession(final String key, final Object value) {
		Struts2Utils.getSession().setAttribute(key, value);
	}

	public static void setObjectToRequest(final String key, final Object value) {
		Struts2Utils.getRequest().setAttribute(key, value);
	}

	public static void setObjectToRequest(final String key) {
		Struts2Utils.getRequest().setAttribute(key, getObjectToRequest(key));
	}

	/**
	 * 从Session中取到值
	 * 
	 * @param value
	 * @return
	 */
	public static ProjectModel getProjectToSession() {
		return (ProjectModel) Struts2Utils.getSessionAttribute("projectModel");
	}

	public static Object getObjectToSession(final String key) {
		return Struts2Utils.getSessionAttribute(key);
	}

	public static Object getObjectToRequest(final String key) {
		return Struts2Utils.getParameter(key);
	}

	/**
	 * 得到taskId getTaskId: 适用:
	 * 
	 * @return
	 * @throws IOException 
	 * @exception
	 * @since 1.0.0
	 */
	public static String getTaskId() throws IOException {
		if (StringUtils.isNotEmpty(Struts2Utils.getParameter("taskId"))) {
			return Struts2Utils.getParameter("taskId");
		}
		if (StringUtils.isNotEmpty((String) Struts2Utils
				.getSessionAttribute("taskId"))) {
			return (String) Struts2Utils.getSessionAttribute("taskId");
		}
		if (StringUtils.isNotEmpty((String) Struts2Utils
				.getSessionAttribute("taskid"))) {
			return (String) Struts2Utils.getSessionAttribute("taskid");
		}
		if (StringUtils.isNotEmpty(Struts2Utils.getParameter("taskid"))) {
			return Struts2Utils.getParameter("taskid");
		}
		return null;
	}

	/**
	 * 得到状态status getStatus: 适用:
	 * 
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public static String getStatus() {
		if (StringUtils.isNotEmpty(Struts2Utils.getParameter("status"))) {
			return Struts2Utils.getParameter("status");
		}
		if (StringUtils.isNotEmpty((String) Struts2Utils
				.getSessionAttribute("status"))) {
			return (String) Struts2Utils.getSessionAttribute("status");
		}
		if (StringUtils.isNotEmpty(Struts2Utils.getParameter("state"))) {
			return Struts2Utils.getParameter("state");
		}
		if (StringUtils.isNotEmpty((String) Struts2Utils
				.getSessionAttribute("state"))) {
			return (String) Struts2Utils.getSessionAttribute("state");
		}
		return null;
	}

	/**
	 * 得到projectId getProjectId: 适用:
	 * 
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public static String getProjectId() {
		if (StringUtils.isNotEmpty(Struts2Utils.getParameter("projectId"))) {
			return Struts2Utils.getParameter("projectId");
		}
		if (StringUtils.isNotEmpty((String) Struts2Utils
				.getSessionAttribute("projectId"))) {
			return (String) Struts2Utils.getSessionAttribute("projectId");
		}
		if (StringUtils.isNotEmpty(Struts2Utils.getParameter("projId"))) {
			return Struts2Utils.getParameter("projId");
		}
		if (StringUtils.isNotEmpty((String) Struts2Utils
				.getSessionAttribute("projId"))) {
			return (String) Struts2Utils.getSessionAttribute("projId");
		}
		return null;
	}
}
